{
 "cells": [
  {
   "cell_type": "raw",
   "id": "938114b2-a0ab-4547-b020-0f414fedc406",
   "metadata": {},
   "source": [
    "### 数组形状操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "01592f26-6647-48a7-88de-79789a8c0057",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "liao_array = np.arange(10)\n",
    "liao_array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "cc6481e8-6746-4a42-89ae-1e6c2797326b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 3, 4],\n",
       "       [5, 6, 7, 8, 9]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "liao_array.shape = 2,5 ### 将数组变成2行5列\n",
    "liao_array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "4fef0f0a-6f0a-4c58-a1b3-39e56d7c2d2b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "liao_array.reshape(1,10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "eba7ead2-6d47-4994-80a1-53e935246667",
   "metadata": {},
   "outputs": [],
   "source": [
    "### 大小不能改变"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "09817a00-18c8-41f1-869d-c4d017549d94",
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "cannot reshape array of size 10 into shape (3,4)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[13], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m liao_array\u001b[38;5;241m.\u001b[39mshape \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m3\u001b[39m,\u001b[38;5;241m4\u001b[39m\n",
      "\u001b[1;31mValueError\u001b[0m: cannot reshape array of size 10 into shape (3,4)"
     ]
    }
   ],
   "source": [
    "liao_array.shape = 3,4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "aeb3dcf4-5a51-4b41-80d5-8dc96ca265d7",
   "metadata": {},
   "outputs": [],
   "source": [
    "### 3行4列，一共有12个元素，而原数组只有10个元素，因此报错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "4f44c3aa-b142-4fd8-a80e-8637fd7d3c16",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(10,)"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "liao_array= np.arange(10)\n",
    "liao_array.shape ### 获取数据的维度和元素个数 (10,) 表示10个元素的一维数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "b20e9b11-279d-4275-9039-8dae777f4a4b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 10)"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "liao_array =  liao_array[np.newaxis,:] ### 表示在第0维增加一个维度 newaxis 表示空值，即只增加维度，不添加具体的数\n",
    "liao_array.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "0ac4d0ce-4e6d-4fb9-9a72-4afa69d8bba3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(10,)"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "liao_array = np.arange(10)\n",
    "liao_array.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "378c507e-230d-4193-bfbb-6e989fc0071d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(10, 1)"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "liao_array = liao_array[:,np.newaxis]\n",
    "liao_array.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "5a0b0be6-75c4-41d4-a616-b0a98fa65e07",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(10, 1, 1, 1, 1, 1)"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "liao_array = liao_array[:,np.newaxis,np.newaxis] ### 由于此时数组的形状为（10，1），因此在最后一个维度增加时，应该变成（10，1，1，1）\n",
    "                                                 ### 即每次增加两列\n",
    "liao_array.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "54b0277a-8105-46c5-8b74-fa124d1a7ab9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(10,)"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "liao_array = liao_array.squeeze() ### squeeae() 将删除数组中所有大小为1的维度，降低维度但不改变数据\n",
    "liao_array.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "638f1ff2-4a1b-47dc-a5bf-14ac414d7bee",
   "metadata": {},
   "outputs": [],
   "source": [
    "liao_array.shape = 2,5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "f6e9ea3d-e854-4865-8464-63dfab98fd53",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 3, 4],\n",
       "       [5, 6, 7, 8, 9]])"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "liao_array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "04c3570f-c52b-4c51-a441-52ff22f7ef14",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 5],\n",
       "       [1, 6],\n",
       "       [2, 7],\n",
       "       [3, 8],\n",
       "       [4, 9]])"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "liao_array.transpose() ### 转置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "772da9d2-a346-42f2-ac37-e2901e210b2f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 5],\n",
       "       [1, 6],\n",
       "       [2, 7],\n",
       "       [3, 8],\n",
       "       [4, 9]])"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "liao_array.T ### 转置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "e176dcf1-bbe1-4f0a-9d03-5adb098e61f9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 3, 4],\n",
       "       [5, 6, 7, 8, 9]])"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "liao_array ### 并不改变原矩阵"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d920d128-04cb-4d53-8a32-a11651cad35e",
   "metadata": {},
   "source": [
    "### 矩阵的连接"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "c449dbd3-79a4-41cc-b3ac-3b12a4b38db3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 123,  456,  789],\n",
       "       [3214,  456,  123]])"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([[123,456,789],[3214,456,123]])\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "7b6a82a9-2aab-45e2-8f00-531efb1ad886",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 123,  234,  234],\n",
       "       [  43,   21, 1234]])"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.array([[123,234,234],[43,21,1234]])\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "b55ffac7-ea44-4b39-aa4f-764c5cb1eb70",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 123,  456,  789],\n",
       "       [3214,  456,  123],\n",
       "       [ 123,  234,  234],\n",
       "       [  43,   21, 1234]])"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = np.concatenate((a,b)) ###沿默认轴（axis=0）拼接：垂直堆叠（对1D数组是延长，对2D数组是向下追加行）\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "e9444578-f9d2-49ca-a12b-6b7886fa7059",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 123,  456,  789],\n",
       "       [3214,  456,  123],\n",
       "       [ 123,  234,  234],\n",
       "       [  43,   21, 1234]])"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = np.concatenate((a,b),axis=0)\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "fde1985b-4066-4510-80da-f8c813c24f7e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 123,  456,  789,  123,  234,  234],\n",
       "       [3214,  456,  123,   43,   21, 1234]])"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = np.concatenate((a,b),axis=1)\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "4618c3e3-7c54-4c02-b84d-875c075f1907",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 6)"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "id": "d2d42026-a4fa-47f1-b949-dceb004f993f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 123,  456,  789],\n",
       "       [3214,  456,  123],\n",
       "       [ 123,  234,  234],\n",
       "       [  43,   21, 1234]])"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.vstack((a,b)) ###将数组按行方向堆叠（沿第一个轴 axis=0）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "id": "05b95d29-b850-4fb2-beb1-b0f2d2b642a0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 123,  456,  789,  123,  234,  234],\n",
       "       [3214,  456,  123,   43,   21, 1234]])"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.hstack((a,b)) ###将数组按列方向堆叠（沿第二个轴 axis=1）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "id": "529f1252-0963-4b03-93a3-21ca4987c710",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 123,  456,  789],\n",
       "       [3214,  456,  123]])"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "id": "9fd7e930-b395-4ce3-a28a-9986774163de",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 123,  456,  789, 3214,  456,  123])"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.flatten()  ### 将任意维度的数组转换为1D数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "id": "88ddcd70-3679-4090-9711-bcd7efb52222",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 123,  456,  789],\n",
       "       [3214,  456,  123]])"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "id": "6301465e-30e5-41d8-99a4-cc6bb39759e8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 123,  456,  789, 3214,  456,  123])"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.ravel()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "id": "5d876747-9d7f-445b-a269-de8d360c97f8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 123,  456,  789],\n",
       "       [3214,  456,  123]])"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e4c2dca4-de85-4124-9b9d-73ed37c00160",
   "metadata": {},
   "outputs": [],
   "source": [
    "###   .flatten() 是有区别的 .ravel()\n",
    "###   需要安全隔离 → 用 flatten() \n",
    "###   需要最高效率 → 用 ravel() （ravel可能改变原数组）"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
